import HeadPrefab from "../../../component/HeadPrefab";
import ComponentBase from "../../../engine/ComponentBase";
import { EventManager } from "../../../engine/EventManager";
import { Logger } from "../../../engine/Logger";
import { SpriteFrameComponent } from "../../../engine/SpriteFrameComponent";
import JhaoScrollViewLayout from "../../../engine/ui/scrollview/JhaoScrollViewLayout";
import UIManager from "../../../engine/UIManager";
import { JhaoProto } from "../../../lib/net/protobuf/jhaoproto";
import { ClubSocketClient } from "../../../net/ClubSocketClient";
import { ClubConfig } from "../../config/ClubConfig";
import { EventConfig } from "../../config/EventConfig";
import { ViewConfig } from "../../config/ViewConfig";
import { UserModel } from "../../model/UserModel";
import { UIHelper } from "../../util/UIHelper";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewClubMember extends ComponentBase {

    private txtMeberNum: cc.Label
    private txtInputUserid: cc.EditBox;

    private users: Array<JhaoProto.IClub_User> = []
    private layout: JhaoScrollViewLayout

    onEnable(): void {
        super.onEnable();
        this.layout = this.getChild(this.node, "meberScrollView/view/content", JhaoScrollViewLayout)
        this.txtMeberNum = this.getChild(this.node, "txtMeberNum", cc.Label);
        this.txtInputUserid = this.getChild(this.node, "txtInputUserid", cc.EditBox);
        let btnSearch: cc.Node = this.findChild(this.node, "btnSearch")
        UIHelper.onAddEvent(btnSearch, () => {
            if (this.txtInputUserid.string == "") {
                // this.getClubMember()
                this.layout.total(this.users.length, this.users)
            } else {
                this.onClickSearch(parseInt(this.txtInputUserid.string))
            }
        })

        EventManager.instance.on(EventConfig.Event_Update_Club_Member, this.getClubMember, this)
        this.getClubMember()

        this.layout.total(this.users.length, this.users)
        this.refresh();
    }

    public deleteMember(member: JhaoProto.IClub_User) {
        for (let i = this.users.length - 1; i >= 0; i--) {
            if(this.users[i].userid == member.userid){
                this.users.splice(i, 1)
                break
            }
        }
        this.layout.total(this.users.length, this.users)
    }

    private getClubMember() {
        ClubSocketClient.req_club_member_list(UserModel.instance.nowClub.clubid, (users: JhaoProto.IClub_User[]) => {
            this.users = users;
            this.refreshMember()
            this.refresh();
        })
    }

    public refreshMember(){
        this.users.sort((a: JhaoProto.IClub_User, b: JhaoProto.IClub_User) => {
            if (a.actor == ClubConfig.Club_Actor_Type.owner) {
                return -1
            }
            else if (b.actor == ClubConfig.Club_Actor_Type.owner) {
                return 1
            }
            return b.actor - a.actor
        })
        this.layout.total(this.users.length, this.users)
    }

    private onClickSearch(userid: number) {
        let showData: JhaoProto.IClub_User = null
        for (let i = 0; i < this.users.length; i++) {
            let data: JhaoProto.IClub_User = this.users[i]
            if (data.userid == userid) {
                showData = data
                break;
            }
        }
        if (showData) {
            this.layout.total(1, [showData])
        }
    }

    private refresh(showData: object = null) {
        this.txtMeberNum.string = "共有(" + this.users.length + ")人"
        this.unscheduleAllCallbacks()

        // if (showData) {
        //     for (let i = 0; i < 1; i++) {
        //         let item: cc.Node = this.itemNodeList[i]
        //         if (!item) {
        //             item = cc.instantiate(this.defaultItemNode)
        //             item.parent = this.defaultItemNode.parent;
        //             this.itemNodeList[i] = item;
        //         }
        //         item.active = true;
        //         this.initOneItem(showData, item)
        //     }
        // }
        // else {
        //     for (let i = 0; i < this.dataList.length; i++) {
        //         let dataObj: object = this.dataList[i];
        //         if (dataObj) {
        //             let item: cc.Node = this.defaultItemNode.parent.children[i]
        //             if (!item) {
        //                 this.scheduleOnce(() => {
        //                     this.initOne(dataObj, i)
        //                 }, 0.02 * i)
        //             } else {
        //                 item.active = true;
        //                 this.initOne(dataObj, i)
        //             }
        //         }
        //     }
        // }
    }

    private refreshItem(item: cc.Node, layerNodeList: Array<cc.Node>, index: number, allList: Array<JhaoProto.IClub_User>) {
        let model: JhaoProto.IClub_User = allList[index]
        if (model) {
            let txtId: cc.Label = this.getChild(layerNodeList[1], "txtId", cc.Label)
            txtId.string = "ID:" + model.userid
            let txtName: cc.Label = this.getChild(layerNodeList[1], "txtName", cc.Label)
            txtName.string = model.nickName
            let head: HeadPrefab = this.getChild(layerNodeList[0], "head", HeadPrefab)
            head.setHead(model.headimgurl)
            let icon_admin: cc.Sprite = this.getChild(layerNodeList[0], "icon_admin", cc.Sprite)
            icon_admin.node.active = false
            if (model.actor == ClubConfig.Club_Actor_Type.owner) {
                icon_admin.node.active = true
                icon_admin.spriteFrame = icon_admin.getComponent(SpriteFrameComponent).spriteFrameList[0]
            } else if (model.actor == ClubConfig.Club_Actor_Type.administrator) {
                icon_admin.node.active = true
                icon_admin.spriteFrame = icon_admin.getComponent(SpriteFrameComponent).spriteFrameList[1]
            } else {
                icon_admin.node.active = true
                icon_admin.spriteFrame = icon_admin.getComponent(SpriteFrameComponent).spriteFrameList[2]
            }
            UIHelper.onAddEvent(item, () => {
                Logger.log("onCLickItem", model)
                UIManager.instance.showView(ViewConfig.viewConfig.ViewClubMemberDetail, null, model, this)
            })
        }
    }



    private initOneItem(data: object, item: cc.Node) {
        let head: HeadPrefab = this.getChild(item, "head", HeadPrefab)
        head.setHead(data["headimg"], data["lv"])
        let txtName: cc.Label = this.getChild(item, "txtName", cc.Label);
        txtName.string = data["nick_name"]
        let txtId: cc.Label = this.getChild(item, "txtId", cc.Label)
        txtId.string = data["userid"]
        let icon_admin: cc.Sprite = this.getChild(item, "icon_admin", cc.Sprite)
        icon_admin.node.active = false
        if (data["actor"] == ClubConfig.Club_Actor_Type.owner) {
            icon_admin.node.active = true
            icon_admin.spriteFrame = icon_admin.getComponent(SpriteFrameComponent).spriteFrameList[0]
        } else if (data["actor"] == ClubConfig.Club_Actor_Type.administrator) {
            icon_admin.node.active = true
            icon_admin.spriteFrame = icon_admin.getComponent(SpriteFrameComponent).spriteFrameList[1]
        } else {
            icon_admin.node.active = true
            icon_admin.spriteFrame = icon_admin.getComponent(SpriteFrameComponent).spriteFrameList[2]
        }
        UIHelper.onAddEvent(item, () => {
            // UIManager.instance.showView(ViewConfig.viewConfig.ViewClubMemberDetail, null, data)
        })
    }

}
